home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / pc / Shout3Ddemo / Shout3d_runtime / codebase / custom_nodes / GradientTexture.java < prev    next >
Text File  |  2000-09-01  |  4KB  |  106 lines

  1. /**    
  2.     Company:        Eyematic Interfaces
  3.     Project:        Shout3D 2.0 Sample Code
  4.     Class:            GradientTexture
  5.     Date:            July 12, 2000
  6.     Description:    A subclass of PixelBasedTexture that generates a texture algorithmically.
  7.     (C) Copyright Eyematic Interfaces, Inc. - 1997-2000 - All rights reserved
  8.  */
  9.  
  10. package custom_nodes;
  11. import shout3d.core.*;
  12. import shout3d.*;
  13. import java.awt.Graphics;
  14.  
  15. /**
  16.  * GradientTexture
  17.  * 
  18.  * A subclass of PixelBasedTexture that generates a texture algorithmically.
  19.  * 
  20.  * Demonstrates how to subclass from PixelBasedTexture and use its
  21.  * API to create a procedurally generated texture.
  22.  * 
  23.  * @author Paul Isaacs
  24.  */
  25.  
  26. public class GradientTexture extends PixelBasedTexture implements FieldObserver {
  27.  
  28.     final public IntField  width    = new IntField(this, "width", Field.NON_NEGATIVE_INT, 2);
  29.     final public IntField  height    = new IntField(this, "height", Field.NON_NEGATIVE_INT, 128);
  30.     final public FloatArrayField  gradientColors    = new FloatArrayField(this, "gradientColors", Field.COLOR_ARRAY, null );
  31.     
  32.     /**
  33.      * Constructs a default GradientTexture node.
  34.      */
  35.     public GradientTexture(){
  36.         width.addFieldObserver(this,null);
  37.         height.addFieldObserver(this,null);
  38.         gradientColors.addFieldObserver(this,null);
  39.     }
  40.     
  41.     public void finalize() throws Throwable {
  42.         width.removeFieldObserver(this);
  43.         height.removeFieldObserver(this);
  44.         gradientColors.removeFieldObserver(this);
  45.         super.finalize();
  46.     }
  47.     
  48.     public void onFieldChange(Field theField, Object userData) {
  49.         // When the fields change, update the pixels.
  50.         if (theField == width || theField == height || theField == gradientColors)
  51.             updateCustomPixels();
  52.     }
  53.  
  54.     /**
  55.      * Generates a texture that is a gradient of colors based on those
  56.      * in the gradientColors field.
  57.      */
  58.     public void updateCustomPixels(){
  59.         if (width.getValue() <= 0 || height.getValue() <= 0 ||
  60.             gradientColors.getValue() == null ||
  61.             gradientColors.getValue().length < 3){
  62.             // fuggedaboudit.
  63.             setCustomPixels(0,0,null,null,null,null);
  64.         }
  65.         else {
  66.             // Generate the texture based on the fields.
  67.             byte[][] myRed = new byte[height.getValue()][width.getValue()];
  68.             byte[][] myGreen = new byte[height.getValue()][width.getValue()];
  69.             byte[][] myBlue = new byte[height.getValue()][width.getValue()];
  70.     
  71.             int numColors = gradientColors.getValue().length / 3;
  72.             byte r, g, b;
  73.             float rowFraction, prevAmount, nextAmount;
  74.             int   prevColor, nextColor;
  75.             float[] gradColors = gradientColors.getValue();
  76.             
  77.             // For each row of the texture
  78.             for (int i = 0; i < height.getValue(); i++){
  79.                 // set rowFraction to a value between 0..1 expressing how far towards maximum height this row is.
  80.                 rowFraction = (float)i/(float)(height.getValue()-1);
  81.                 // find colors before (or at) and after (or at) this row
  82.                 prevColor = (int) (rowFraction * (numColors-1));
  83.                 nextColor = (prevColor == numColors-1) ? prevColor : prevColor + 1;
  84.                 // Amounts of each to mix to ramp smoothly from prevColor to nextColor
  85.                 prevAmount = nextColor - rowFraction*(numColors-1);
  86.                 nextAmount = (prevColor==nextColor) ? 1 : rowFraction*(numColors-1) - prevColor;
  87.                 
  88.                 // Mix some of each color to get the gradient color
  89.                 // Color values in the pixel array need to be bytes ranging 0-255
  90.                 r = (byte)(255* (gradColors[3*prevColor]   * prevAmount + gradColors[3*nextColor]   * nextAmount));
  91.                 g = (byte)(255* (gradColors[3*prevColor+1] * prevAmount + gradColors[3*nextColor+1] * nextAmount));
  92.                 b = (byte)(255* (gradColors[3*prevColor+2] * prevAmount + gradColors[3*nextColor+2] * nextAmount));
  93.                 // Set all pixels in the row to this color.
  94.                 for (int j = 0; j < width.getValue(); j++){
  95.                     // Color values are bytes ranging 0-255
  96.                     myRed[i][j]   = r;
  97.                     myGreen[i][j] = g;
  98.                     myBlue[i][j]  = b;
  99.                 }
  100.             }
  101.             // Set the pixels, alpha channel is null.
  102.             setCustomPixels(width.getValue(), height.getValue(), 
  103.                             myRed, myGreen, myBlue, null);
  104.         }
  105.     }
  106. }